package com.neusoft.neuiotms.dcom.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementResultDTO;
import com.neusoft.neuiotms.dcom.dto.ShiftDTO;
import com.neusoft.neuiotms.dcom.model.enums.ShiftType;
import com.neusoft.neuiotms.dcom.service.CalendarService;
import com.neusoft.neuiotms.dcom.service.DutyArrangementResultService;
import com.neusoft.neuiotms.dcom.service.ShiftService;

@Service
public class CalendarServiceImpl implements CalendarService {

    @Autowired
    private DutyArrangementResultService dutyArrangementResultService;
    @Autowired
    private ShiftService shiftService;

    @Override
    public List<DutyArrangementResultDTO> myCalendar(final Searchable searchable, final UserBean user) {

        final List<DutyArrangementResultDTO> dtos = new ArrayList<>();

        // 查询值班安排
        searchable.put("userName", user.getUsername());
        final List<DutyArrangementResultDTO> results = this.dutyArrangementResultService.index(searchable);

        // 查询替班情况
        searchable.put("sourceUserCode", user.getUsername());
        searchable.put("status", true);
        final List<ShiftDTO> shiftRemoveList = this.shiftService.index(searchable);

        // 别人换成自己
        final Searchable searchable1 = new Searchable();
        searchable1.put("targetUserCode", user.getUsername());
        searchable1.put("shiftStartTime", searchable.getLongValue("startTime"));
        searchable1.put("shiftEndTime", searchable.getLongValue("endTime"));
        searchable1.put("status", true);
        searchable1.put("type", ShiftType.shift);
        final List<ShiftDTO> shiftRemoveList1 = this.shiftService.index(searchable1);

        final List<Long> removeIds = new ArrayList<>();

        results.forEach(k -> {
            shiftRemoveList.stream().forEach(m -> {
                // 移除掉替换班数据
                if ((k.getStartTime().getTime() == m.getStartTime().getTime())
                        && (k.getEndTime().getTime() == m.getEndTime().getTime())) {
                    removeIds.add(k.getId());
                }
            });
            shiftRemoveList1.stream().forEach(m -> {
                // 移除掉替换班数据
                if ((k.getStartTime().getTime() == m.getShiftStartTime().getTime())
                        && (k.getEndTime().getTime() == m.getShiftEndTime().getTime())) {
                    removeIds.add(k.getId());
                }
            });
        });

        // 移除掉被别人替班的数据
        results.stream().filter(s -> !removeIds.contains(s.getId())).forEach(dtos::add);

        // 替别人的班
        final Searchable searchable2 = new Searchable();
        searchable2.put("targetUserCode", user.getUsername());
        //        searchable2.put("type", ShiftType.substitute);
        searchable2.put("startTime", searchable.getLongValue("startTime"));
        searchable2.put("endTime", searchable.getLongValue("endTime"));
        searchable2.put("status", true);
        final List<ShiftDTO> shiftList = this.shiftService.index(searchable2);
        shiftList.stream().forEach(k -> {
            searchable.put("userName", k.getSourceUserCode());
            searchable.put("eqStartTime", k.getStartTime().getTime());
            searchable.put("eqEndTime", k.getEndTime().getTime());
            final List<DutyArrangementResultDTO> list = this.dutyArrangementResultService.index(searchable);
            list.forEach(dtos::add);
        });

        //查询我换的班
        final Searchable searchable3 = new Searchable();
        searchable3.put("sourceUserCode", user.getUsername());
        searchable3.put("type", ShiftType.shift);
        searchable3.put("shiftStartTime", searchable.getLongValue("startTime"));
        searchable3.put("shiftEndTime", searchable.getLongValue("endTime"));
        searchable3.put("status", true);
        final List<ShiftDTO> shiftList1 = this.shiftService.index(searchable3);

        shiftList1.stream().forEach(k -> {
            searchable.put("userName", k.getTargetUserCode());
            searchable.put("eqStartTime", k.getShiftStartTime().getTime());
            searchable.put("eqEndTime", k.getShiftEndTime().getTime());
            final List<DutyArrangementResultDTO> list = this.dutyArrangementResultService.index(searchable);
            list.forEach(dtos::add);
        });

        return dtos;
    }

}
